Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internal): handle leading whitespace in message content #2083

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

blingblin-g
Copy link

@blingblin-g blingblin-g commented Feb 4, 2025

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

This PR fixes an issue in the GPT structured output streaming functionality where unexpected leading whitespace (e.g., \n\n) in the API response causes an EOF while parsing a value error. The change ensures that leading and trailing whitespace in choice_snapshot.message.content is stripped before parsing.
The updated code modifies the following section in openai/lib/streaming/chat/_completions.py :

if (
    choice_snapshot.message.content.strip()
    and not choice_snapshot.message.refusal
    and is_given(self._rich_response_format)
):
    choice_snapshot.message.parsed = from_json(
        bytes(choice_snapshot.message.content, "utf-8"),
        partial_mode=True,
    )

This prevents the EOF error during streaming caused by improperly formatted responses.

Additional context & links

  • Problem: The API sometimes returns structured JSON responses with unexpected leading whitespace, such as \n\n{"foo": "bar"} instead of the expected {"foo": "bar"}. This causes the from_json function to fail during parsing.
    Root Cause: The original code does not handle leading or trailing whitespace in choice_snapshot.message.content before evaluating or parsing it.
    Solution: By adding .strip() to choice_snapshot.message.content, the whitespace is removed, ensuring proper parsing of the JSON response.
    This fix has been tested locally and resolves the issue without introducing side effects.If there are any concerns about this change or its potential impact, please feel free to share them.

issue #2082

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant